package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.dao.BookShelfMapper;
import com.lwy.dao.BookStateMapper;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookNovelChapter;
import com.lwy.entity.BookShelf;
import com.lwy.entity.BookState;
import com.lwy.service.BookShelfService;
import com.lwy.util.ChapterFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

//我的书架service接口实现类
@Service
public class BookShelfServiceImpl extends ServiceImpl<BookShelfMapper, BookShelf> implements BookShelfService {

    @Resource
    private BookNoveServiceImpl bookNoveService;

    @Resource
    private BookNovelChapterServiceImpl bookNovelChapterService;

    @Resource
    private BookStateMapper bookStateMapper;

    //    小说id
    public List<BookNove> listAll(){
        List<BookNove> id=null;
        List<BookShelf> bookShelves = baseMapper.selectList(null);
        for (BookShelf bookShelf : bookShelves) {
             id= bookNoveService.list(new QueryWrapper<BookNove>().eq("id", bookShelf.getNovel_id()));
        }
        return id;
    }


    public List<BookShelf> findByList(Integer user_id){
        List<BookShelf> shelfList = baseMapper.selectList(new QueryWrapper<BookShelf>().eq("user_id", user_id).eq("status",1));
        for (BookShelf bookShelf : shelfList) {
            List<BookNovelChapter> id1 =null;
            List<BookNove> id = bookNoveService.list(new QueryWrapper<BookNove>().eq("id", bookShelf.getNovel_id()));
            for (BookNove bookNove : id) {
                BookState byOrder = bookStateMapper.findByOrder(user_id, bookNove.getId());
                if(null==byOrder){
                    id1 = bookNovelChapterService.firstList(bookNove.getId());
                    for (BookNovelChapter bookNovelChapter : id1) {
                        ChapterFilter.setEntity(bookNovelChapter);
                    }
                }else{
                    id1= bookNovelChapterService.list(new QueryWrapper<BookNovelChapter>().eq("id", byOrder.getNovel_chapter_id()));
                    for (BookNovelChapter bookNovelChapter : id1) {
                        ChapterFilter.setEntity(bookNovelChapter);
                    }
                }

            }
            BookNovelChapter bookNovelChapter = bookNovelChapterService.newList(bookShelf.getNovel_id());
            bookShelf.setList(id);
            bookShelf.setChapterList(id1);
            bookShelf.setBookNovelChapter(bookNovelChapter);
        }
        return shelfList;
    }


}

